import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.File;


public class GameUI extends Application {
    static Label stepShow = new Label(""+Constants.Step);//步数显示
    static Text timeShow = new Text();//用时显示
    static eventAction eventaction;//事件对象
    Constants constants;//必要值
    ImageView imageView;//游戏图片
    GameUI(Image image){
        constants = new Constants(image);
        imageView = new ImageView(image);
    }
    @Override
    public void start(Stage stage) {
        stage.setResizable(false);
        if (StartUI.range.set.get(4).compareTo("1") == 0) {
            constants.game.play();
        }
        stepShow.setFont(Font.font("宋体", FontWeight.BOLD, 24));
        timeShow.setFont(Font.font("宋体", FontWeight.BOLD, 24));
        Label label = new Label("步数");
        label.setFont(Font.font("宋体", FontWeight.BOLD, 24));
        Label label1 = new Label("时间");
        label1.setFont(Font.font("宋体", FontWeight.BOLD, 24));
        Button reset = new Button("重玩");
        reset.setFont(Font.font("宋体", FontWeight.BOLD, 17));
        reset.setOnAction(event -> {
            try {
                Constants.win.stop();
                eventaction.buttonaction.handle2(event);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        VBox vBox=new VBox(label,stepShow,label1,timeShow,reset);
        vBox.setSpacing(20);
        Button button[]=new Button[3];
        button[0] = new Button("暂停游戏");
        button[0].setFont(Font.font("宋体", FontWeight.BOLD, 17));
        button[0].setOnAction(event -> {
            eventaction.buttonaction.handle1(event);
        });
        button[1] = new Button("查看图片");
        button[1].setFont(Font.font("宋体", FontWeight.BOLD, 17));
        button[1].setOnAction(event -> {
            eventaction.buttonaction.handle1(event);
        });
        button[2] = new Button("返回游戏");
        button[2].setFont(Font.font("宋体", FontWeight.BOLD, 14));
        button[2].setOnAction(event -> {
            eventaction.buttonaction.handle1(event);
        });
        Stage stage1 = new Stage();//查看图片的界面
        setStage(stage1, button[2]);
        eventaction = new eventAction(button, stage, stage1);
        HBox hBox = new HBox();
        hBox.setPadding(new Insets(25, 10, 15, 120));
        hBox.setSpacing(50);
        hBox.getChildren().addAll(button[0],button[1]);//装入按键
        HBox hBox1 = new HBox();
        hBox1.setPadding(new Insets(10, 20, 30, 20));
        hBox1.setSpacing(50);
        hBox1.getChildren().addAll(constants.gridPane,vBox);
        BorderPane borderPane = new BorderPane();
        borderPane.setCenter(hBox1);//设置游戏区域
        borderPane.setTop(hBox);//设置按键区域
        ImageView back = new ImageView(StartUI.background);
        back.setOpacity(0.5);
        StackPane stackPane = new StackPane(back, borderPane);
        Scene scene = new Scene(stackPane, 460, 420);
        stage.setScene(scene);
        stage.setTitle("拼图游戏");
        stage.show();
    }

    void setStage(Stage stage,Button button) {
        stage.setResizable(false);
        Label label1 = new Label("  完成图");
        Label label2 = new Label("\t\t\t     缺失块");
        label1.setFont(Font.font("宋体", FontWeight.BOLD,17));
        label2.setFont(Font.font("宋体", FontWeight.BOLD,17));
        HBox hBox1 = new HBox();
        hBox1.setPadding(new Insets(0, 10, 5, 80));
        hBox1.setSpacing(70);
        hBox1.getChildren().addAll(label1, label2);//装入标签
        HBox hBox2 = new HBox();
        hBox2.setPadding(new Insets(20, 20, 0, 190));
        hBox2.getChildren().add(button);
        FlowPane flowPane = new FlowPane();
        flowPane.getChildren().addAll(hBox2, hBox1);//按键和标签
        flowPane.setOrientation(Orientation.VERTICAL);
        flowPane.setVgap(10);
        flowPane.setPrefWrapLength(100);
        HBox hBox = new HBox();
        hBox.setPadding(new Insets(0, 20, 30, 20));
        hBox.setSpacing(20);
        hBox.getChildren().addAll(constants.gridPane1, constants.imageViews1[StartUI.level*StartUI.level-1]);//图片显示区域
        BorderPane borderPane = new BorderPane();
        borderPane.setTop(flowPane);
        borderPane.setCenter(hBox);
        ImageView back = new ImageView(StartUI.background);
        back.setOpacity(0.75);
        StackPane stackPane = new StackPane(back, borderPane);
        Scene scene = new Scene(stackPane, 460, 420);
        stage.setTitle("拼图游戏");
        stage.setScene(scene);
    }
}